package Messages;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import ocsf.server.ConnectionToClient;
import server.SQLConnection;
/**
 * This class implements the Add flower to DB
 * @author Gabi Revah
 * @author Aviad Almog
 */
public class MessageAddFlower {
	
	private ConnectionToClient client ;
	private String[] str; 
	
/**
 * constructor of this class 
 * @param str
 * @param client
 */
	public MessageAddFlower(String[] str, ConnectionToClient client) {
		this.client= client ;
		this.str= str; 	
	}
	
/**
 * Adding Flower to the DB
 * @throws SQLException
 * @throws IOException
 */
	public void AddFlower() throws SQLException, IOException{
		SQLConnection sc = new SQLConnection();
		java.sql.PreparedStatement fl = sc.getConnection().prepareStatement("SELECT * FROM flowers where name='"+str[1]+"' and color='"+str[2]+"';");
		ResultSet flrs= fl.executeQuery();
		if(flrs.next()){
			ArrayList<Object> bus=new ArrayList<Object>();
			bus.add("FlowerExist");		
			client.sendToClient(bus);
		}
		
		else{
		java.sql.PreparedStatement flins = sc.getConnection().prepareStatement("INSERT INTO flowers (name,color,height,suppID,inventorystock,rentstock) VALUES(?,?,?,?,?,?);");
			
			flins.setString(1,str[1]);					
			flins.setString(2,str[2]);
			flins.setString(3,str[3]);
			flins.setString(4,str[4]);
			flins.setString(5,str[5]);
			flins.setString(6,"0");
			flins.executeUpdate();
			flins.close();
		
			
								ArrayList<Object> bus=new ArrayList<Object>();
								bus.add("AddFlower");		
								client.sendToClient(bus);
		}
	}
	
}
